<?php
class ControllerCfContactus extends Controller {

    private $error = array();
    
    /**
     * 
     * List out
     * @return unknown_type
     */
    public function index() 
    {        
        $this->language->load('common/contactus');
        $lang_items_key = array(
            'text_form_title', 'text_form_fullname', 'text_form_email', 'text_form_telephone', 
            'text_form_address', 'text_form_message', 'text_form_erase', 'text_form_send', 'text_page_address'
        );
        
        $this->data = array_merge($this->data, $this->language->gets($lang_items_key));
        
        if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->request->post['submit']) {
            if ($this->validateAndSend()) {             
                if ($this->send($this->data['form'])) {
                    $this->redirect('');    
                }
                else {
                    //                      
                }                                
            }
            else {
                //
            }
        }

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/contactus.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/contactus.tpl';
		} else {
			$this->template = 'default/template/common/contactus.tpl';
		}        
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    }
    
    
    // -------------------------------------------------------------------------------------------
        
    /**
     * validate form data & send
     * @return 1: complete - 2: valid fail - 3: send fail 
     */
    private function validateAndSend()
    {        
        if (trim($this->request->post['fullname']) == '') {
            $this->error['email'] = $this->language->get('text_err_fullname_required');            
        }
        
        if (trim($this->request->post['message']) == '') {
            $this->error['message'] = $this->language->get('text_err_message_required');            
        }
        
        if (count($this->error) > 0) 
            return 2;
        else {
            // save data to DB
                        
        }
    }
}